package authoritah.ui.event;

import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import authoritah.db.Attendance;
import authoritah.db.Member;
import authoritah.utils.TimeUtils;


public class EventAttendanceTableModel extends DefaultTableModel {

	private String[] columnHeaders;
	private ArrayList<Attendance> rowData;
	
	public EventAttendanceTableModel() {
		columnHeaders = new String[]{"Member", "Arrived", "Left", "Pts"};
		rowData = new ArrayList<Attendance>();
	}
	
	public EventAttendanceTableModel(ArrayList<Attendance> rowData) {
		columnHeaders = new String[]{"Member", "Arrived", "Left", "Pts"};
		this.rowData = rowData;
	}

	public int getColumnCount() {
		return columnHeaders.length;
	}

	public String getColumnName(int column) {
		return columnHeaders[column];
	}

	public int getRowCount() {
		if (rowData != null)
		{
			return rowData.size();
		}
		else
		{
			return 0;
		}
	}

	public Object getValueAt(int row, int column) {
		Attendance attendance = rowData.get(row);
		switch (column) {
			case 0:
				return attendance.getMember();
			case 1:
				return attendance.getArrive();
			case 2:
				return attendance.getLeave();
			case 3:
				return TimeUtils.calculatePoints(attendance.getArrive(), attendance.getLeave());
		}
		return "";
	}

	public boolean isCellEditable(int row, int column) {
		switch (column)
		{
			case 0:
			case 1:
			case 2:
				return true;
		}
		return false;
	}

	public void setValueAt(Object value, int row, int column) {
		Attendance attendance = rowData.get(row);
		switch (column) {
			case 0:
				attendance.setMember((Member)value);
			case 1:
				attendance.setArrive(TimeUtils.parseTime((String)value));
			case 2:
				attendance.setLeave(TimeUtils.parseTime((String)value));
		}
	}

	public void updateData(ArrayList<Attendance> rowData) {
		this.rowData = rowData;
		fireTableDataChanged();
	}

	public ArrayList<Attendance> getAttendanceList() {
		return rowData;
	}
}
